package com.fht.service.food.impl;

import com.fht.entity.FoodCategory;
import com.fht.entity.MerchantsInformation;
import com.fht.enums.ManagermentSystemEnum;
import com.fht.exception.ManagermentSystemException;
import com.fht.from.FoodCategoryFrom;
import com.fht.mapper.food.FoodCategoryMapper;
import com.fht.service.food.FoodCategoryService;
import com.fht.service.food.MerchantsInformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

import java.util.*;

@Service
@Slf4j
public class FoodCategoryServiceImpl implements FoodCategoryService {

    @Resource
    public FoodCategoryMapper foodCategoryMapper;
    @Resource
    public MerchantsInformationService merchantsInformationService;

    @Override
    public List<FoodCategory> findParents(FoodCategoryFrom from) {

        Example example = new Example(FoodCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId",from.getShopId());
        c.andEqualTo("parentId","0");
        if (! "".equals(from.getName()) && null!=from.getName()){
            c.andLike("parentName","%"+from.getName()+"%");
        }

        List<FoodCategory> parentFoodcategorieList = foodCategoryMapper.selectByExample(example);
        for (FoodCategory foodCategory : parentFoodcategorieList) {
            String childNames="";
            Example example1 = new Example(FoodCategory.class);
            Example.Criteria c1 = example1.createCriteria();
            c1.andEqualTo("merchantsId",from.getShopId());
            c1.andEqualTo("parentId",foodCategory.getId());
            List<FoodCategory> foodCategories = foodCategoryMapper.selectByExample(example1);

            if (foodCategories.size()>0) {
                for (FoodCategory category : foodCategories) {
                    childNames = childNames + category.getChild() + "、";
                }
                childNames = childNames.substring(0, childNames.lastIndexOf("、"));
                foodCategory.setChildNames(childNames);
            }else {
                foodCategory.setChildNames("暂无数据");
            }
        }




        return parentFoodcategorieList;
    }

    @Override
    public Map<String,Object> findById(String id) {

        Example example = new Example(FoodCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("parentId",id);
        List<FoodCategory> childFoodCategoryList = foodCategoryMapper.selectByExample(example);

        FoodCategory parentFoodCategory = foodCategoryMapper.selectByPrimaryKey(id);

        MerchantsInformation merchantsInformation = merchantsInformationService.selectById(parentFoodCategory.getMerchantsId());

        parentFoodCategory.setMerchantsName(merchantsInformation.getName());

        Map<String,Object> map = new HashMap<>();
        map.put("parentFoodCategory",parentFoodCategory);
        map.put("childFoodCategoryList",childFoodCategoryList);

        return map;
    }

    @Override
    public FoodCategory selectById(String id) {
        return foodCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(FoodCategory foodCategory) {

        FoodCategory oldFoodCategory = foodCategoryMapper.selectByPrimaryKey(foodCategory.getId());
        if (oldFoodCategory!=null ){
            FoodCategory parent = new FoodCategory();
            parent.setId(Integer.valueOf(oldFoodCategory.getParentId()));
            parent.setParentName(foodCategory.getParentName());
            foodCategoryMapper.updateByPrimaryKeySelective(parent);

            foodCategory.setParentName(null);

            foodCategoryMapper.updateByPrimaryKeySelective(foodCategory);

        }


    }

    @Override
    @Transactional
    public void delelteById(String id) {
        foodCategoryMapper.deleteByPrimaryKey(id);
        Example example = new Example(FoodCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("parentId",id);
        foodCategoryMapper.deleteByExample(example);
    }

    @Override
    public List<FoodCategory> selectBymerchantsId(String merchantsId) {
        Example example = new Example(FoodCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId",merchantsId);
        c.andEqualTo("parentId","0");
        List<FoodCategory> foodCategoryList = foodCategoryMapper.selectByExample(example);

        return foodCategoryList;
    }

    @Override
    @Transactional
    public FoodCategory addParent(String merchantsId, String newCategoryName) {

        FoodCategory foodCategory = new FoodCategory();
        foodCategory.setMerchantsId(merchantsId);
        foodCategory.setParentName(newCategoryName);
        foodCategory.setParentId("0");

        Example example = new Example(FoodCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("parentName",newCategoryName);
        c.andEqualTo("merchantsId",merchantsId);
        List<FoodCategory> foodCategoryList = foodCategoryMapper.selectByExample(example);
        if (foodCategoryList.size()>0){
            throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
        }

        int i = foodCategoryMapper.insertSelective(foodCategory);
        if (i!=1){
            throw new ManagermentSystemException(ManagermentSystemEnum.APPINFO_ADD_ERROR);
        }

        return foodCategory;
    }

    @Override
    public void addChild(FoodCategory foodCategory) {

        foodCategory.setParentId(foodCategory.getParentName());
        foodCategory.setParentName(null);

        Example example = new Example(FoodCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("parentId",foodCategory.getParentId());
        c.andEqualTo("child",foodCategory.getChild());
        List<FoodCategory> foodCategoryList = foodCategoryMapper.selectByExample(example);
        if (foodCategoryList.size()>0){
            throw new ManagermentSystemException(ManagermentSystemEnum.FOOD_CATEGORY_ADD_REPETITION);
        }


        foodCategoryMapper.insertSelective(foodCategory);

    }

    @Override
    public Map<String, Object> findParentsAndChildsById(Integer id) {

        FoodCategory foodCategory = foodCategoryMapper.selectByPrimaryKey(id);

        Example example1 = new Example(FoodCategory.class);
        Example.Criteria c1 = example1.createCriteria();
        c1.andEqualTo("parentId","0");
        c1.andEqualTo("merchantsId",foodCategory.getMerchantsId());
        List<FoodCategory> parents = foodCategoryMapper.selectByExample(example1);

        List<FoodCategory> childs = this.findByParentId(foodCategory.getParentId());
        Map<String , Object> map = new HashMap<>();
        map.put("categoryParentId",foodCategory.getParentId());
        map.put("parents",parents);
        map.put("childs",childs);

        return map;
    }

    @Override
    public List<FoodCategory> findByParentId(String parentId) {
        Example example = new Example(FoodCategory.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("parentId",parentId);
        List<FoodCategory> childs = foodCategoryMapper.selectByExample(example);
        return childs;
    }
}
